package com.liangzhong.modules.trainingcamp.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.TrainingcampEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxyconstant.trainingcamp.TrainingcampConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.trainingcamp.ITrainingcamp;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import lz.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *训练营 
	 * @author Administrator
	 * 
	 */	
	public class TrainingcampMediator extends ModuleMediator
	{
		public static const MediatorName :String = "TrainingcampMediator";
		public function TrainingcampMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get clubView():ITrainingcamp
		{
			return viewComponent as ITrainingcamp;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().dispatchEvent(new TrainingcampEvent(TrainingcampEvent.REMOVE_MODEL));
			(clubView as DisplayObject).addEventListener(TrainingcampEvent.REMOVE_MODEL,onRemoveModeHanlder);
			(clubView as DisplayObject).addEventListener(TrainingcampEvent.START_TRAINING,onStartTrainingHandler);
			(clubView as DisplayObject).addEventListener(TrainingcampEvent.UP_TRAININGCAMP,onupTrainingHandler);
			(clubView as DisplayObject).addEventListener(TrainingcampEvent.CLEAR_UP_TIME,onclearTimeHandler);
			(clubView as DisplayObject).addEventListener(TrainingcampEvent.BUY_TRAININGTIME,onbuyHandler);
		}
		
		protected function onbuyHandler(event:TrainingcampEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(TrainingcampConstant.TRAININGCAMPE_MEDIATORTOCOMMAND,event.param,TrainingcampConstant.BUY_TRAINING_TIME);
		}
		
		/**
		 *清除冷却时间 
		 * @param event
		 * 
		 */
		protected function onclearTimeHandler(event:TrainingcampEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(TrainingcampConstant.TRAININGCAMPE_MEDIATORTOCOMMAND,event.param,TrainingcampConstant.CLEAR_UP_TIME);
		}
		
		protected function onupTrainingHandler(event:TrainingcampEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(TrainingcampConstant.TRAININGCAMPE_MEDIATORTOCOMMAND,event.param,TrainingcampConstant.UP_TRAININGCAMP);
		}
		
		/**
		 *开始训练 
		 * @param event
		 * 
		 */
		protected function onStartTrainingHandler(event:TrainingcampEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(TrainingcampConstant.TRAININGCAMPE_MEDIATORTOCOMMAND,event.param,TrainingcampConstant.START_TRAINING);
		}
		
		protected function onRemoveModeHanlder(event:TrainingcampEvent):void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [TrainingcampConstant.GET_START_TRAINING,
				TrainingcampConstant.GET_TRAININGCAMPINFO,
				TrainingcampConstant.GET_UP_TRAININGCAMP,
//				TrainingcampConstant.GET_CLEAR_UP_TIME,
				NotificationConstants.COMMON_DROP_TIME,
				NotificationConstants.PLAYER_UPGRADE].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case TrainingcampConstant.GET_START_TRAINING:
					clubView.training(notification.getBody());
					break;
				case TrainingcampConstant.GET_TRAININGCAMPINFO:
					if(viewComponent == null && ResourceManager.instance.isContainResource(ResourceConst.TRAININGCAMP_SWF))
						showUI();
					if(viewComponent != null)
					{
						clubView.training(notification.getBody());
					}
					break;
				case TrainingcampConstant.GET_UP_TRAININGCAMP://升级训练营
					clubView.uptraining(notification.getBody());
					break;
				case TrainingcampConstant.GET_CLEAR_UP_TIME:
					
					break;
				case NotificationConstants.COMMON_DROP_TIME://返回清除冷却时间
					if(notification.getBody() == BuildingType.TRAIN)
					{
						AppData.getInstance().trainingCampDataManager.trainingcampInfo.pretime = 0;
						clubView.clearTime(notification.getBody());
						Alert.showErrorTip("冷却成功");
					}
					
					break;
				case NotificationConstants.PLAYER_UPGRADE:
					if(viewComponent != null)
						clubView.playerUP();
					break;
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			if(AppData.getInstance().trainingCampDataManager.trainingcampInfo == null || AppData.getInstance().clientHourse == 22)
				this.sendNotification(TrainingcampConstant.TRAININGCAMPE_MEDIATORTOCOMMAND,null,TrainingcampConstant.TRAININGCAMPINFO);
			resName = ResourceConst.TRAININGCAMP_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.TRAININGCAMP_SWF))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).trainingcamp[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.TRAININGCAMP_SWF,resList);
			if(viewComponent == null && AppData.getInstance().trainingCampDataManager.trainingcampInfo != null)
				showUI();
//			showUI();
		}
		/**
		 * 
		 * 
		 */
		private function showUI():void
		{
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.trainingcamp.TrainingcampView");
			cView.initUI();
			viewComponent = cView;
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}